********************************************************************************
* 
* Table 2: Effects of Merger on Inputs, Financials, and Clinical Outcomes 
*
********************************************************************************


*******
******* Settings and directories 
*******

** specify an output directory 
local fpath_output "/homes/nber/shruthi-dua51934/sacarny-DUA51934/shruthi-dua51934/replication_files/output_20230606"

** directory containing the main analytic file
local fpath_build_output = "/disk/agedisk4/medicare.work/sacarny-DUA51934/shruthi-dua51934/replication_files/build/output"

** directory ccontaining himss supplemental variables 
local fpath_himss = "/disk/agedisk4/medicare.work/sacarny-DUA51934/shruthi-dua51934/replication_files/himss/input/"

** directory containing physician flows supplemental data 
local fpath_physician_flows = "/disk/agedisk4/medicare.work/sacarny-DUA51934/shruthi-dua51934/replication_files/physician_flows/output/"

** replication analysis directory 
local fpath_analysis = "/disk/agedisk4/medicare.work/sacarny-DUA51934/shruthi-dua51934/replication_files/analysis_20230606/"


*******
******* Which sections of code to run?
*******
local prepare_data = 1
local run_regressions = 0
local combine_tables = 0
local clean_tables = 0


*******
******* Specify RHS variables 
*******
local main_vars "discvendcount wcost_entrants wcost_exits wcost_churn logfte lw_capinv_tot ladjcosts_w ldafny_price05 ladjrev_w profit_margin_w shdx rhdx"




*******
******* read and assemble the data 
******
if `prepare_data' == 1{
	
	* bring in the hospital-year panel  
	use "`fpath_build_output'/acq_cleaned_complete_20230606.dta", clear 

	
	* clean up
	label var hospbd "Total beds"

	* reformat urbancbsa variable: 1 if rural 
	bys id: egen urban2 = max(urbancbsa)
	replace urbancbsa = urban2 
	replace urbancbsa = 1 - urbancbsa 
	label var urbancbsa "Share rural hospitals"

	* drop if missing hrrcode
	drop if missing(hrrcode)

	* numeric variable for the aha id 
	egen id2 = group(id)


	* total cost per adjusted discharge 
	gen adj_costs = totcost / ((1000000)*ipdischarges_adultped*(1 + (opcharge/ipcharge)))
	label var adj_costs "Adjusted costs per IP discharge (\$1 millions)" 
	winsor2 adj_costs, cuts(05 95) by(year)

	* total revenue per adjusted discharge 
	gen adj_revenue = income / ((1000000)*ipdischarges_adultped*(1 + (opcharge/ipcharge)))
	label var adj_revenue "Adjusted revenue per IP discharge (\$1 millions)"
	winsor2 adj_revenue, cuts(05 95) by(year)

	* log costs and revenue 
	gen ladjcosts_w = log((adj_costs_w))
	label var ladjcosts_w "Log(costs/bed)"

	gen ladjrev_w = log((adj_revenue_w))
	label var ladjcosts_w "Log(revenue/bed)"

	* Winsorize dafny price index 
	winsor2 dafny_price, cuts(05 95) by(year)
	gen ldafny_price05 = log(dafny_price_w)
	label var ldafny_price05 "log(Dafny price index)"

	* winsorize profit margin in each year
	winsor2 profit_margin, cuts(05 95) by(year) label

	* capital investment 
	replace capinv_tot = capinv_tot/1000000
	label var capinv_tot "Capital investment \$millions"
	
	* winsorize and log  capital investment
	winsor2 capinv_tot, cuts(05 95) by(year) label
	gen lw_capinv_tot = log(1+capinv_tot_w)
	label var lw_capinv_tot "log(1+winsorized capital investment)"


	* scale FTE counts by beds 
	gen fte_per_bed = fte/hospbd
	label var fte_per_bed  "Full time employees per bed"

	* create log(fte)
	gen logfte = log(fte)
	label var logfte "log(FTE)"
	label var fte "Full Time Employees"


	********
	******** for the difference-in-difference analysis
	******** 
	**  restrict the sample: Legacy, Target, Other forprofits 
	keep if forprofit == 1 
	drop if target2 == 1 | acq_other == 1 

	label var ind08_11acq_legacy "Acquirer * 08-11"
	label var ind12_14target "Target * 12-14"

	* define post-treatment 
	gen postm = ( year >= 2008)
	gen postm_target = postm * target 
	gen postm_legacy = postm * acq_legacy 
	label var postm_target "Post 2008 * Target"
	label var postm_legacy "Post 2008 * Acquirer"

	* define interim
	gen interim = (year == 2007)
	gen interim_target = interim * target
	gen interim_legacy = interim * acq_legacy

	label var interim_target "Target 2007"
	label var interim_legacy "Acquirer 2007"

	* sanity check... 
	assert target2 + acq_other == 0 

	* event study
	* generate year dummies 
	tab year, gen(yr)

	*interact year dummies with acq legacy/ target indicators  
	foreach v of varlist yr* {
		gen target_`v' = target * `v'	
		gen legacy_`v' = acq_legacy * `v'

	}	

	* clean up the labels on group * year interactions 
	foreach v of varlist target_yr* {
		local num = subinstr("`v'", "target_yr", "", 1) 
		local `num' %4.0f `num'
		local num = `num' + 2002
		label var `v' "Target * `num'"
	}


	foreach v of varlist legacy_yr* {
		local num = subinstr("`v'", "legacy_yr", "", 1) 
		local `num' %4.0f `num'
		local num = `num' + 2002
		label var `v' "Acquirer * `num'"
	}	

	* drop dummy for 2006 (t-1)
	drop *yr4

	* gen year * hrr FEs
	egen yrhrr = group(year hrrcode)

	* gen year * HRR TREND
	gen yr_hrr_trend = year * hrrcode
}





******* 
*******  run the regressions 
*******  
if `run_regressions' == 1 {
	
	
	
foreach v in `main_vars' {
	di "****** Variable: `v'" 
	preserve
	
	** vendor count
	if "`v'" == "discvendcount"{ 
		* bring in the vendor distance info 
		merge 1:1 id year using "`fpath_himss'/vendor_distance_indiv2003_2014_v2.dta", keep(match)
		gen discvendcount = distlegacy^2
	}
	
	** drop 2008 if physician flows variable (NPI to UPIN change)
	if inlist("`v'", "wcost_entrants", "wcost_exits", "wcost_churn"){
		merge 1:1 id year using "`fpath_physician_flows'/physician_flows_data2003_2014", keepusing(wpat* wcost* *_sh nphyshosp) keep(match) nogen 
		drop if year == 2008
	}
	
	** drop 2003 for capital investment results 
	if "`v'" == "lw_capinv_tot" {
		drop if year == 2003
	}
	
	** begin regressions
	** note that surv and readm outcomes are run in a separate file 
	if inlist("`v'", "shdx", "rhdx") {
		quietly do "`fpath_analysis'/Table2_main_survreadm20230606.do"	
	}
	
	if !inlist("`v'", "shdx", "rhdx") {
		* main 
		reghdfe `v' postm_legacy postm_target interim_legacy interim_target  if forprofit == 1, absorb(year id2) vce(cluster id2)		
		regsave using "`fpath_output'/`v'_main_20230606", p  addlabel(rhs, "`v'", spec, "main") replace 
	
	
	} /*end regressions */ 
		
	restore 
}


}





*******
******* combine tables 
*******
if `combine_tables' == 1 {
	clear
	save "`fpath_output'/main_tables_comb_20230606.dta", emptyok replace 

	foreach v in `main_vars' {
	
		clear
	
		di"`v'" 
		
		use "`fpath_output'/`v'_main_20230606", clear 
		append using "`fpath_output'/main_tables_comb_20230606.dta"
		save "`fpath_output'/main_tables_comb_20230606.dta", replace 		
	
	}

}




******* 
******* clean up the tables and save to excel 
******* 
if `clean_tables' == 1 {
	
	use "`fpath_output'/main_tables_comb_20230606", clear
	drop if strpos(var, "_cons") | strpos(var, "interim") | strpos(var, "hrr")

	replace var = strtitle(subinstr(var, "postm_", "", 1))
	replace var = "Acquirer" if strpos(var, "Legacy")
	replace var = "Target" if strpos(var, "Target")
	
	cap drop _id
	cap drop r2 spec 
	foreach v in coef stderr pval {
		replace `v' = round(`v', .001)
	}

	gen asterisk = "***" if pval <= .01
	replace asterisk = "**" if pval > .01 & pval <= .05
	replace asterisk = "*" if pval >.05 & pval < .1
	gen paren1 = "("
	gen paren2 = ")"

	egen coef2 = concat(coef asterisk)
	egen stderr2 = concat(paren1 stderr paren2)

	drop coef stderr paren1 paren2 asterisk pval
	rename coef2 coef
	rename stderr2 stderr 

	reshape wide coef stderr , i(rhs N) j(var) string
	order N, last 

	rename rhs depvar

	rename coefAcquirer post_Acquirer
	rename coefTarget post_Target
	rename stderrAcquirer se_Acquirer
	rename stderrTarget se_Target

	replace depvar = "Discordant vendor count" if depvar == "discvendcount"
	replace depvar = "Log of costs per inpatient" if depvar == "ladjcosts_w"
	replace depvar = "Log of revenue per inpatient" if depvar == "ladjrev_w"
	replace depvar = "Log of price index" if depvar == "ldafny_price05"
	replace depvar = "Log of FTE" if depvar == "logfte"
	replace depvar = "Log of 1 + capital investment" if depvar == "lw_capinv_tot"
	replace depvar = "Profit margin" if depvar == "profit_margin_w"
	replace depvar = "Readmission" if depvar == "rhdx"
	replace depvar = "Survival" if depvar == "shdx"
	replace depvar = "Physician churn rate" if depvar == "wcost_churn"
	replace depvar = "Physician entry rate" if depvar == "wcost_entrants"
	replace depvar = "Physician exit rate" if depvar == "wcost_exits"

	gen order = 1 if depvar == "Discordant vendor count"
	replace order = 2 if depvar == "Physician entry rate"
	replace order = 3 if depvar == "Physician exit rate" 
	replace order = 4 if depvar == "Physician churn rate" 
	replace order = 5 if depvar == "Log of FTE" 
	replace order = 6 if depvar == "Log of 1 + capital investment" 
	replace order = 7 if depvar == "Log of costs per inpatient"
	replace order = 8 if depvar == "Log of price index" 
	replace order = 9 if depvar == "Log of revenue per inpatient" 
	replace order = 10 if depvar == "Profit margin" 
	replace order = 11 if depvar == "Survival" 
	replace order = 12 if depvar == "Readmission" 
	sort order 

	drop order 

	export excel using "`fpath_output'/Table2_main.xls", sheet("`spec'", replace) firstrow(varlabel)


}




